from django.shortcuts import render, redirect
from myapp.utils.Pagination import Pagination
from myapp import models
from myapp.utils.form import PretyModelform, PretyUpdateModelForm


def prety_list(request):
    """靓号列表"""
    # for i in range(40):
    #     models.PretyNum.objects.create(mobile='18228831233',price=12,level=1,status=2)

    # """分页逻辑"""
    # 每页展示数据起始值和结束值
    # page = int(request.GET.get('page',1))
    # page_size = 8
    # start = (page-1)* page_size
    # end = page * page_size
    # """翻页标签"""
    # count = models.PretyNum.objects.count()#总条数
    # page_count,y = divmod(count,page_size)#求数据库总页数
    # if y >0:
    #     page_count +=1
    # # 当前页码小于size 控制标签边界值
    # size = 3
    # start_page = page - size
    # end_page = page + size
    # # 数据库数据库很少情况：
    # if page_count<2*size+1:
    #     start_page =1
    #     end_page = page_count
    # else:#数据库数据比较多
    #     # 当前页小于等于size
    #     if  page<=size:
    #         start_page =1
    #         end_page = size * 2 +1
    #         # 当前页码加size大于总页数
    #     elif (page + size) >page_count:
    #         start_page = page_count-2*size
    #         end_page = page_count
    #
    # page_list=[]
    # #上一页
    # if page >1:
    #       prev =  '<li ><a href="?page={}">上一页</a></li>'.format(page-1)
    # else:
    #       prev = '<li ><a href="?page={}">上一页</a></li>'.format(1)
    # page_list.append(prev)
    # # 页码
    # for i in range(start_page,end_page+1):
    #     if i == page:
    #        ela = ('<li class="active"><a href="?page={}">{}</a></li>'.format(i, i))
    #     else:
    #        ela= ('<li><a href="?page={}">{}</a></li>'.format(i,i))
    #     page_list.append(ela)
    # # 下一页
    # if page < page_count:
    #     prev = '<li ><a href="?page={}">上一页</a></li>'.format(page + 1)
    # else:
    #     prev = '<li ><a href="?page={}">上一页</a></li>'.format(page_count)
    # page_list.append(prev)
    # # 跳转页
    # go_to_str = """                    <form method="get" style="float: left">
    #                     <div class="input-group" style="width:100px;">
    #                         <input type="text" value="{}" name="page" class="form-control" placeholder="页码">
    #                         <span class="input-group-btn">
    #             <button class="btn btn-default" type="submit">》</button>
    #           </span>
    #                     </div>
    #                     </form>""".format(page)
    # page_list.append(go_to_str)
    #
    # page_str = mark_safe(''.join(page_list))
    # #搜索功能
    # serach_dict = {}
    # search_data = request.GET.get('search_data', '')
    # if search_data:
    #     serach_dict['mobile__contains'] = search_data
    # 展示各类条件下的数据 order_by('-level')用来排序加-就是desc

    """ 搜索功能 """
    serach_dict = {}
    search_data = request.GET.get('search_data', '')
    if search_data:
        serach_dict['mobile__contains'] = search_data
    # ------------------------------------------------------------------------
    data_set = models.PretyNum.objects.filter(**serach_dict).order_by('id')  # 查询的分页数据
    page_object = Pagination(request, query_set=data_set, size=2)  # 实例化一个分页类
    page_str = page_object.html()  # 返回翻页html标签
    context = {  # 用字典放给模板的各个数据
        'data_set': page_object.data_queryset,  # 分完页的数据
        'search_data': serach_dict,  # 搜索条件
        'page_str': page_str,  # 页码
    }
    return render(request, 'prety_list.html', context)


def prety_add(request):
    if request.method == "GET":
        form = PretyModelform()
        return render(request, 'prety_model_form_add.html', {'form': form})
    form = PretyModelform(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect('/prety/list/')
    return render(request, 'prety_model_form_add.html', {'form': form})


def prety_update(request, nid):
    rows_data = models.PretyNum.objects.filter(id=nid).first()
    if request.method == 'GET':
        form = PretyUpdateModelForm(instance=rows_data)
        return render(request, 'prety_update.html', {'form': form})
    form = PretyUpdateModelForm(instance=rows_data, data=request.POST)
    if form.is_valid():
        form.save()
        return redirect('/prety/list/')
    return render(request, 'prety_update.html', {'form': form})


def prety_delete(request, nid):
    models.PretyNum.objects.filter(id=nid).delete()
    return redirect('/prety/list/')
